<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="util" %>

<jsp:include page="../fragments/header.jsp">
	<jsp:param name="title" value="Order List" />
</jsp:include>

<c:choose>
		<c:when test="${empty orders}">
			<p>There are no <b>${title}</b> Purchase Orders <c:if test="${operation=='SEARCH'}">meeting your search criteria</c:if>.</p>
		</c:when>
		<c:otherwise>
			<p>
				<c:choose>
					<c:when test="${operation=='SEARCH'}">Search results for <b>${title}</b> Purchase Orders</c:when>
					<c:otherwise>List of <b>${title}</b> Purchase Orders</c:otherwise>	
				</c:choose>
			</p>
			<br/>

			<table class="table table-hover">
				<thead>
					<tr>
						<th>#</th>
						<th>Created By</th>
						<th>Status</th>
						<th>Actions</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${orders}" var="order">
						<tr>
							<td><a href="${pageContext.request.contextPath}/po_details?id=${order.key.id}">${order.key.id}</a></td>
							<td>${order.key.creator.username}</td>
							<td>${order.key.status.friendlyName}</td>
							<td>
								<c:choose>
									<c:when test="${not empty order.value}">
										<c:forEach var="toStatus" items="${order.value}">
											<util:change-status
												url="${pageContext.request.contextPath}/po_status"
												order_id="${order.key.id}"
												from="${order.key.status}" 
												to="${toStatus}"
												text="Set as <b>${toStatus.friendlyName}</b>" />
										</c:forEach>
									</c:when>
									<c:otherwise>
										No actions available.
									</c:otherwise>
								</c:choose>
							</td>
						</tr>
					</c:forEach>
				</tbody>
			</table>

			<c:if test="${operation != 'SEARCH'}">
				<form action="${pageContext.request.contextPath}/po_search_page" method="POST">
					<input type="hidden" name="title" value="${title}">
					<input type="submit" class="btn btn-default btn-block" value="Search Orders">
				</form>
			</c:if>	

		</c:otherwise>
	</c:choose>

	<c:if test="${title=='Shipped'}">
		<form action="${pageContext.request.contextPath}/po_check_shipped" method="POST">
			<input type="button" class="btn btn-default btn-block" value="Check If New Shipped POs">
		</form>
	</c:if>

	<c:if test="${operation == 'SEARCH'}">
		<form action="${pageContext.request.contextPath}/po_list_back_all">
			<button class="btn btn-default">
				<b>Back to All Orders List</b>
			</button>
		</form>
	</c:if>

<jsp:include page="../fragments/footer.jsp" />